/*
 ×操作订单操作
 */
;(function($){
	 $(function(){
    var $orderid=$("input[name='orderid']").val();
    $.ajax({
        type:"POST",
        url:"/supply.php/Home/Order/orderAction",
        data:{"orderid":$orderid},
        dataType:"json",
        success:function(message){
          if (message.status) 
            {
               var $logs=message.logs;
               var  $tbody=$('<tbody></tbody>');
               var $string='';
               $.each($logs,function(key,log){
                $string+="<tr>";
                $string+="<td>"+log.action_user+"</td>";
                $string+="<td>"+log.edittime+"</td>";
                var $status=parseInt(log.status);
                switch($status)
                {
                    case 0:
                      $string+="<td>未确认</td>";
                      $string+="<td>未付款</td>";
                      $string+="<td>未发货</td>";
                      $string+="<td>"+log.action_note+"</td>";
                      break;
                    case 2:
                      $string+="<td>未确认</td>";
                      $string+="<td>未付款</td>";
                      $string+="<td>未发货</td>";
                      $string+="<td>订单已取消</td>";
                      break;
                      case 1:
                      $string+="<td>已确认</td>";
                      $string+="<td>未付款</td>";
                      $string+="<td>未发货</td>";
                       $string+="<td>"+log.action_note+"</td>";
                      break;
                      case 3:
                      $string+="<td>已确认</td>";
                      $string+="<td>已付款</td>";
                      $string+="<td>未发货</td>";
                       $string+="<td>"+log.action_note+"</td>";
                      break;
                      case 6:
                      $string+="<td>已确认</td>";
                      $string+="<td>已付款</td>";
                      $string+="<td>配货中</td>";
                       $string+="<td>"+log.action_note+"</td>";
                      break;
                      case 8:
                      $string+="<td>已确认</td>";
                      $string+="<td>已付款</td>";
                      $string+="<td>已发货</td>";
                       $string+="<td>"+log.action_note+"</td>";
                      break;
                      case 9:
                      $string+="<td>已确认</td>";
                      $string+="<td>已付款</td>";
                      $string+="<td>已收货</td>";
                       $string+="<td>"+log.action_note+"</td>";
                      break;
                
                } 
                
                
                $string+="</tr>"; 
               });
               $tbody.append($string);
               $($tbody).replaceAll(".orderlog tbody");

            }else{
                return '';
            }
        }
    })
  })
  /*
   *发货模块
   */
  $('#delivery').click(function(){
      var $this=$(this);
      var $logistic_num=$("input[name='logistic_num']").val(); //物流单号 
      var $orderid=$("input[name='orderid']").val();
      var $delivery=$("select[name='delivery'] option:selected").val() ; //物流单号 
      if ($logistic_num.length<=0 || $delivery.length<=0  || $orderid.length<=0) { alert('必须填写物流公司和物流单号');return false;};

       $.ajax({
      type:'POST',
      url:"/supply.php/Home/Order/ajaxDelivery",
      data:{'orderid':$orderid,'logistic_num':$logistic_num,"delivery":$delivery},
      dataType:'json',
      success:function(mess){
        var status=mess.status;
        if (status !=false) 
        {
          $this.attr('class','');
          alert('修改订单状态成功');
          window.location.reload()
        }else{
          alert(mess.msg);
        }
       
      }
    })
  })
})(jQuery)
;(function($){
  $('#expressline').click(function(){
    document.getElementById("retData").innerHTML="<center>正在查询物流信息，请稍后...</center>";
　　var expressid =$('.shipping_name').attr('value');　
    var expressno =$('.expressno').attr('value');　

    $.ajax({
       type:'GET',
       url:"/supply.php/Home/Order/express",
       data:{'com':expressid,"nu":expressno},
       dataType:"text",
       success:function(message){
            if (message ==false) 
              {
                 $('暂无数据').replaceAll("#retData");
                  $('.result').html('跟踪物流结果');
                  return false;
              };
            var myiframe=message;
            $(message).replaceAll("#retData");
            $('.result').html('跟踪物流结果');

       }
    });
  });
　　

　　

})(jQuery)
/*
 ×ajax 修改供应商的零售价、进货价、库存
 */
;(function($){
  $.ajaxEdit= {
    
    url:'', //操作url
    init:function(url) 
    { //初始化 
      this.url = url;
      return this;
    },
    edit:function(obj,action,val,id)
    { //ajax编辑
      var $this = obj;
      var $id=$this.attr('id');
      var $action=$this.attr('value');
      if($this.find("input").length == 0){
        var txt    = $this.text(),
          size   = parseInt(txt.length),
          $span  = $("<input type = 'text' value='"+txt+"' size='"+ (size>70?70:size) +"'/>");
          
          setValue = function()
          { //向服务器提交修改后的数据
              var spanval = $span.val();
              if(spanval && spanval != txt)
              {
                
                $.ajax({
                  type:'POST',
                  url:"/supply.php/Home/Item/ajaxEditItem",
                  data:{"spanval":spanval,"itemid":$id,"action":$action},
                  dataType:'json',
                  success: function(message)
                  {    
                    //$.util.hide();

                    if(message.status)
                    { //数据更新成功
                      $this.text(spanval);
                      
                    }else
                    { //数据更新失败
                      $this.text(txt);
                      alert(message.msg);
                    }
                    
                  }
                });

              } 
              else 
              {
                $this.text(txt);
              }
          }
        $this.html($span);
        $span.focus().select();
        $span.keyup(
          function(event)
          { 
            if(event.keyCode == "13")
            {
              setValue();

            }
        }).blur(function(){
          setValue();
          //location.reload();
        });
      }
      else {
        return ;
      }
    },

    bgcolor:function(obj,type) {
      $(obj).css('backgroundColor',type=='on' ? '#0b819f':'');
    },

  }

  $(document).on('click','.itemprice,.supprice,.inventory',function(){
    var $this=$(this);
    //调用Ajax修改
    $.ajaxEdit.edit($this);
  })

})(jQuery)

/*
 商品模块 ajax 修改商品规格
 */
;(function($){
  $.ItemLayer=
  {
    addLayer:function(e)
    {
         var itemid=(e!=false)?e:0; //商品ID 
         //获取商品的颜色，价格，库存，名称，型号
         $.ajax
         (
         {
          type:'post',
          url:"/supply.php/Home/Item/ajaxProduct",
          data:{'itemid':itemid},
          dataType:"json",
          success:function(message)
          {
            var $tbody=$('<tbody></tbody>');
            var $string='';
            var status=message.status;
            var $products=message.products;
            if(status)
            {

              $.each($products,function(key,product){
                $string+='<tr><td>'+product.productid+'</td>';
                $string+='<td> <span class="prodcutcolor" value="prodcutcolor" id="'+product.productid+'">'+ product.color+'</span></td>';
                $string+='<td> <span class="prodcutprice" value="prodcutprice" id="'+product.productid+'">'+ product.price+'</span></td>';
                $string+='<td> <span class="prodcutsupprice" value="prodcutsupprice" id="'+product.productid+'">'+ product.supprice+'</span></td>';
                $string+='<td> <span class="productinventory" value="productinventory" id="'+product.productid+'">'+ product.inventory+'</span></td>';
                $string+='</tr>';
              })
                        //设置弹出层的itemid 的值
                        $("input[name='itemidvalue']").val(itemid);
                        $tbody.append($string);
                        $($tbody).replaceAll(".protable tbody");

                    }else{
                      var $tbody=$('<tbody>暂无规格</tbody>');
                      $($tbody).replaceAll(".protable tbody");
                    }
                }
            }
            );         
          }, //弹出product结束

        //ajax 直接修改规格start
        url:'', //操作url
        init:function(url) 
    { //初始化 
      this.url = url;
      return this;
    },
    edit:function(obj)
    { //ajax编辑
      var $this = obj;
      var $id=$this.attr('id');
      var $action=$this.attr('value');
      //获取商品itemid
      var $itemid= $("input[name='itemidvalue']").val();
      if($this.find("input").length == 0){
        var txt    = $this.text(),
        size   = parseInt(txt.length),
        $span  = $("<input type = 'text' value='"+txt+"' size='"+ (size>70?70:size) +"'/>");

        setValue = function()
          { //向服务器提交修改后的数据
            var spanval = $span.val();
            if(spanval && spanval != txt)
            {

              $.ajax({
                type:'POST',
                url:"/supply.php/Home/Item/ajaxEditproduct",
                data:{"spanval":spanval,"productid":$id,"action":$action,'itemid':$itemid},
                dataType:'json',
                success: function(message)
                {    
                    //$.util.hide();

                    if(message.status)
                    { //数据更新成功
                      $this.text(spanval);
                      //更新基础库存
                      var itemid=message.itemid; //商品ID
                      var iteminven= message.suminven;//库存
                      $('.item_'+itemid+' .inventory').html(iteminven);
                      
                    }else{ //数据更新失败
                      $this.text(txt);
                      alert(message.msg);
                    }
                    
                  }
                });

            } 
            else 
            {
              $this.text(txt);
            }
          }
          $this.html($span);
          $span.focus().select();
          $span.keyup(
            function(event)
            { 
              if(event.keyCode == "13")
              {
                setValue();

              }
            }).blur(function(){
              setValue();
          //location.reload();
        });
          }
          else {
            return ;
          }
        },

        bgcolor:function(obj,type) {
          $(obj).css('backgroundColor',type=='on' ? '#0b819f':'');
        },
          //ajax 直接修改规格end

      };
    /* 为动态添加product 添加事件*/
      $(document).on('click','.prodcutcolor,.prodcutprice,.prodcutsupprice,.productinventory',function(){
        var $this=$(this);
    //调用Ajax修改
    
    $.ItemLayer.edit($this);
  })

})(jQuery)